﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class Admin_QuanLyTinTuc : System.Web.UI.Page
{
    TinTucCtr tinTucCtr = new TinTucCtr();
    TimKiemCtr timKiemCtr = new TimKiemCtr();
    public void checkAlreadyLogin()
    {
        if ((Session["UserType"].ToString() != "QuanTri" && Session["UserType"].ToString() != "NhanVien") || Session["UserID"].ToString() == null||Session["QuanTri"].ToString()==null)
            Response.Redirect("Login.aspx");
    }
    private void layTinTuc()
    {
        gvTinTuc.DataSource = tinTucCtr.layTinTucH();
        gvTinTuc.DataBind();
        MultiView1.ActiveViewIndex = 0;
    }
    private void layLoaiTintuc()
    {
        cbloaitin.DataSource = tinTucCtr.layLoaiTinTuc();
        cbloaitin.DataTextField = "TenLoai";
        cbloaitin.DataValueField = "MaLoaiTT";
        cbloaitin.DataBind();
    }
    private void hienThiTinTuc(int maTT)
    {
        ETinTuc TinTuc = tinTucCtr.laytinTucH(maTT.ToString());
        hdmatt.Value = maTT.ToString();
        lbloidungluong.Text = "";
        tbtieude.Text = TinTuc.TieuDe.ToString();
        txtMoDau.Text = TinTuc.MoDau.ToString();
        txtnoidung.Text = TinTuc.Noidung.ToString();
        cbloaitin.SelectedValue = TinTuc.LoaiTin.ToString();
        cbAn.Checked = bool.Parse(TinTuc.TrangThai.ToString());
        imageMinhHoa.ImageUrl = "../Admin/images/QC/" + TinTuc.HinhAnh.ToString();
        hdhinh.Value = TinTuc.HinhAnh.ToString();
        MultiView1.ActiveViewIndex = 1;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        checkAlreadyLogin();
        if (!IsPostBack)
        {
            if (Request.QueryString["ChuoiTimKiem"] == "")
            {
                layTinTuc();
            }
            else
            {
                string tieuDe = Request.QueryString["ChuoiTimKiem"];
                gvTinTuc.DataSource = timKiemCtr.TimKiemTinTucTheoTen(tieuDe); 
                gvTinTuc.DataBind();
                MultiView1.ActiveViewIndex = 0;
            }
        }

    }
    protected void gvTinTuc_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvTinTuc.PageIndex = e.NewPageIndex;
        layTinTuc();
    }
    
    protected void btThemmoi_Click(object sender, EventArgs e)
    {
        btcapnhat.Visible = false;
        lbloidungluong.Text = "";
        btthem.Visible = true;
        regularFileHinh1.Visible = true;
        MultiView1.ActiveViewIndex = 1;
        layLoaiTintuc();
    }
    protected void btthem_Click(object sender, EventArgs e)
    {
        lbloinoidung.Text = "";
        lbloidungluong.Text = "";
        lbMoDau.Text = "";
        if (txtnoidung.Text == "")
            lbloinoidung.Text = "*";
        if (txtMoDau.Text == "")
            lbMoDau.Text = "*";
        else
        {
            HttpPostedFile myFile = flhinh.PostedFile;
            int nFileLen = myFile.ContentLength;
            if (nFileLen > 5242880)
            {
                lbloidungluong.Text = "File quá lớn, Cần <5MB"; //Giới hạn file
                MultiView1.ActiveViewIndex = 1;
            }
            else
            {
                ETinTuc tinTuc = new ETinTuc();
                //tinTuc.MaTT = int.Parse(hdmatt.Value);
                tinTuc.TieuDe = tbtieude.Text.Trim();
                tinTuc.MoDau = txtMoDau.Text.Trim();
                tinTuc.LoaiTin = int.Parse(cbloaitin.SelectedValue);
                tinTuc.Noidung = txtnoidung.Text.Trim();
                tinTuc.HinhAnh = flhinh.FileName;
                tinTuc.Quantri = "admin";
                if (cbAn.Checked)
                {
                    tinTuc.TrangThai = true.ToString();
                }
                else
                {
                    tinTuc.TrangThai = false.ToString();
                }

                bool flag = tinTucCtr.InsertTinTucH(tinTuc);
                flhinh.PostedFile.SaveAs(Server.MapPath("../Admin/images/QC/") + flhinh.FileName);
                layTinTuc();
                if (flag)
                {
                    Response.Write("<script >alert('Thêm Tin Tức Thành Công');window.location=\"QuanLyTinTuc.aspx?ChuoiTimKiem=\"</script>");

                }
                else
                {
                    Response.Write("<script >alert('Thêm Tin Tức Thất Bại');window.location=\"QuanLyTinTuc.aspx?ChuoiTimKiem=\"</script>");
                }
            }
        }
    }
    protected void btcapnhat_Click(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(flhinh.FileName))
        {
            HttpPostedFile myFile = flhinh.PostedFile;
            int nFileLen = myFile.ContentLength;
            if (nFileLen > 5242880)
            {
                lbloidungluong.Text = "File quá lớn,Cần <5MB";//Giới hạn file
                MultiView1.ActiveViewIndex = 1;
            }
            else
            {
                ETinTuc TinTuc = new ETinTuc();
                TinTuc.MaTT=int.Parse(hdmatt.Value);
                TinTuc.TieuDe=tbtieude.Text.Trim();
                TinTuc.MoDau = txtMoDau.Text.Trim();
                TinTuc.LoaiTin=int.Parse(cbloaitin.SelectedValue);
                TinTuc.Noidung=txtnoidung.Text.Trim();
                TinTuc.HinhAnh=flhinh.FileName;
                TinTuc.TrangThai=cbAn.Checked.ToString();
                bool flag = tinTucCtr.UpdateTinTucH(TinTuc);
                     
                flhinh.PostedFile.SaveAs(Server.MapPath("../Admin/images/QC/") + flhinh.FileName);
                imageMinhHoa.ImageUrl = "../Admin/images/QC/" + flhinh.FileName;
                if (flag)
                {
                    Response.Write("<script language=\"javascript\" type\"text/javascript\">alert('Cập Nhật Tin Tức Thành Công');window.location=\"QuanLyTinTuc.aspx?ChuoiTimKiem=\"</script>");

                }
                else
                {
                    Response.Write("<script language=\"javascript\" type\"text/javascript\">alert('Cập Nhật Tin Tức Thất Bại');window.location=\"QuanLyTinTuc.aspx?ChuoiTimKiem=\"</script>");
                }
            }
        }
        else
        {
            imageMinhHoa.ImageUrl = "../Admin/images/QC/" + hdhinh.Value;
            ETinTuc TinTuc = new ETinTuc();
            TinTuc.MaTT = int.Parse(hdmatt.Value);
            TinTuc.TieuDe = tbtieude.Text.Trim();
            TinTuc.MoDau = txtMoDau.Text.Trim();
            TinTuc.LoaiTin = int.Parse(cbloaitin.SelectedValue);
            TinTuc.Noidung = txtnoidung.Text.Trim();
            TinTuc.HinhAnh = hdhinh.Value;
            TinTuc.TrangThai = cbAn.Checked.ToString();
            bool flag = tinTucCtr.UpdateTinTucH(TinTuc);
            if (flag)
            {
                Response.Write("<script language=\"javascript\" type\"text/javascript\">alert('Cập Nhật Tin Tức Thành Công');window.location=\"QuanLyTinTuc.aspx?ChuoiTimKiem=\"</script>");

            }
            else
            {
                Response.Write("<script language=\"javascript\" type\"text/javascript\">alert('Cập Nhật Tin Tức Thất Bại');window.location=\"QuanLyTinTuc.aspx?ChuoiTimKiem=\"</script>");
            }
        }
    }
    protected void gvTinTuc_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    protected void gvTinTuc_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton btn = (LinkButton)e.Row.Cells[5].Controls[1];
            //khi click vao nut xoa se phat sinh doan ma javascript thong bao xac nhan co muon xoa hay khong
            btn.OnClientClick = "return confirm('Bạn có chắc chắn xóa tin tức : " + Server.HtmlDecode(e.Row.Cells[1].Text) + " này không?')";
        }
    }
    protected void gvTinTuc_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName.Equals("Edit"))
            {
                int maLoai = int.Parse(e.CommandArgument.ToString());

                //string maLoai = gvPhanLoai.SelectedRow.Cells[1].Text;
                layLoaiTintuc();
                btcapnhat.Visible = true;
                btthem.Visible = false;
                regularFileHinh1.Visible = false;
                hienThiTinTuc(maLoai);
              
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
       
    }
    protected void gvTinTuc_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int matt = int.Parse(gvTinTuc.Rows[e.RowIndex].Cells[0].Text);
        ETinTuc TinTuc = tinTucCtr.laytinTucH(matt.ToString());
        //if (bool.Parse(TinTuc.TrangThai.ToString()) == true)
        //{
        //    lbloi.Text = "Tin tức đang đăng !không được xóa!!";
        //}
        //else
        //{
            bool flag=tinTucCtr.DeleteTinTucH1(matt.ToString());
            
            if (flag)
            {
               // File.Delete(Server.MapPath("../Admin/images/QC/") + TinTuc.HinhAnh.ToString());
                Response.Write("<script >alert('Xóa Tin Tức Thành Công');window.location=\"QuanLyTinTuc.aspx?ChuoiTimKiem=\"</script>");

            }
            else
            {
                Response.Write("<script >alert('Xóa Tin Tức Thất Bại');window.location=\"QuanLyTinTuc.aspx?ChuoiTimKiem=\"</script>");
            }
            layTinTuc();
        //}
    }
}